/******************************************************************************
 * @File name   :      CardDefinitionDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 11, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 11, 2015 6:02:26 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.card.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao;
import com.cap.bts.crmdp.dealer.card.domain.CardDefinition;
import com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Pager;
import com.cap.bts.framework.common.util.WhereConditionGenerator;

/**
 *
 */
@Repository("cardDefineDao")
public class CardDefineDaoImpl extends BaseDaoImpl<CardDefinition, Long>implements ICardDefineDao {

    /**
     * @Date        :      Aug 11, 2015
     * @param clazz
     */
    @Inject
    public CardDefineDaoImpl(@Value("com.cap.bts.crmdp.dealer.card.domain.CardDefinition")Class<CardDefinition> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao#getCardDefinitionListByDto(com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto)
    **/
    @Override
    public List<CardDefinition> getCardDefinitionListByDto(CardDefineQueryDto dto) throws AppException {
       String hql = "select a from CardDefinition a";
       String countHql = "select count(*) ";

       String fromStr = "from CardDefinition a";
       String whereStr = " where 1=1";
       
       WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);
       
        if(dto.getQueryStatus()!=null){
            condition.and("a.status", "=", dto.getQueryStatus());
        }
        
        String orderHql = " order by a.id desc";
        
        Pager page = dto.getPager();
        
        page.setPageSize(10);//test
       
       return getListByQueryWithPaging(hql + condition.toString()+ orderHql, countHql + fromStr + condition.toString(), dto.getPager());
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao#getCardDefinitionListByDto(com.cap.bts.crmdp.dealer.card.dto.CardDefineQueryDto)
    **/
    @Override
    public List<CardDefinition> getGenerateDefinitionListByDto(CardDefineQueryDto dto) throws AppException {
       String hql = "select a from CardDefinition a";
       String countHql = "select count(*) ";

       String fromStr = "from CardDefinition a";
       String whereStr = " where a.isPhysicalCard=1";
       
       WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);
       
        if(dto.getQueryStatus()!=null){
            condition.and("a.status", "=", dto.getQueryStatus());
        }
        
        String orderHql = " order by a.id desc";
        
        Pager page = dto.getPager();
        
        page.setPageSize(10);//test
       
       return getListByQueryWithPaging(hql + condition.toString()+ orderHql, countHql + fromStr + condition.toString(), dto.getPager());
    }
    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao#updateCardDefinition(com.cap.bts.crmdp.dealer.card.domain.CardDefinition)
    **/
    @Override
    public void updateCardDefinition(CardDefinition cd) throws AppException {
        CardDefinition cdp=super.findById(cd.getId());
        cdp.setStatus(cd.getStatus());
        super.update(cdp);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao#addCardDefinition(com.cap.bts.crmdp.dealer.card.domain.CardDefinition)
    **/
    @Override
    public CardDefinition addCardDefinition(CardDefinition cd) throws AppException {
        return super.save(cd);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 12, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao#deleteCardDefinition(com.cap.bts.crmdp.dealer.card.domain.CardDefinition)
    **/
    @Override
    public void deleteCardDefinition(CardDefinition cd) throws AppException {
         super.remove(cd.getId());
    }
    /**
     * 
     * @Date        :      Aug 25, 2015
     * @param dto
     * @return
     * @throws AppException
     */
    @Override
    public Integer getCardDefinitionCount(CardDefineQueryDto dto) throws AppException {
        StringBuilder sb =new StringBuilder();
        sb.append("select count(*) from CardDefinition a where 1=1 ");
        if(dto.getPerfix()!=null){
            sb.append("and a.perfix='"+dto.getPerfix()+"'");
        }
        if(dto.getName()!=null){
            sb.append("and a.name='"+dto.getName()+"'");
        }
        return super.countByQuery(sb.toString());
    }

    /**
     * 查询所有的卡片信息
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardDefineDao#getCardList()
     */
    @Override
    public List<CardDefinition> getCardList() throws AppException {
        String sqlString = "select * from tm_crm_card_definition where status='1'";
        return  super.getListByNativeQueryWithVaParam(sqlString);
    }
}
